//
//  SinaVistorView.swift
//  weibo
//
//  Created by sirius on 15/12/31.
//  Copyright © 2015年 sirius. All rights reserved.
//

import UIKit

protocol SinaVistorViewDelegate: NSObjectProtocol {
    func didLoginMethod()
    func didRegisterMethod()
}




class SinaVistorView: UIView {
    weak var delegate: SinaVistorViewDelegate?
    
    
    //构造函数调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
   //MARK: --添加到视图里
    private func setupUI(){
        //改变 背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        addSubview(animationImageView)
        addSubview(maskImageView)
        addSubview(houseImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //添加约束
        
        
        
        //首页动画，图片约束，水平垂直约束
        //关闭系统的Autoresizing
        animationImageView.translatesAutoresizingMaskIntoConstraints = false
        houseImageView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        maskImageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: animationImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: animationImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //设置朦层的约束
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -100))
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: -350))

        

        
        

        
        
        
        //首页的小房子约束
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //提示label,水平居中，宽度，距离顶部是他的顶部
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: animationImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        //注册按钮 宽 20 高 30，左边和message对齐，顶部和底部对齐
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
          addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        //loginBtn
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
        
    }
    //MARK: --动画方法
    func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置属性 
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        //当完成动画后，试图跑到后台后，该属性是否保留
        animation.removedOnCompletion = false
        animationImageView.layer.addAnimation(animation, forKey: nil)
    }
    //MARK: 定义一个方法，供子类去调用
    func setupInfo(message: String,image: String?){
        //通过设置图片为可选项，判断首页是否需要显示轮播动画图片
        if let Oimage = image{
            animationImageView.hidden = true
            houseImageView.image = UIImage(named: Oimage)
        }else{
            animationImageView.hidden = false

        }
//        houseImageView.image = UIImage(named: image)
        messageLabel.text = message
    }
    //MARK:__登录注册按钮点击事件
    func registerMethod(){
        printLog("注册")
        delegate?.didRegisterMethod()
    }
    func loginMethod(){
        printLog("登录")
        delegate?.didLoginMethod()
        
    }
     //MARK: --懒加载
    private lazy var maskImageView:UIImageView = {
     let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return img
    }()
   //MARK: --懒加载
    private lazy var animationImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        img.sizeToFit()
        return img
    }()
    //首页小房子
   private lazy var houseImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        return img
    }()
    //提示语
   private lazy var messageLabel: UILabel = {
        let lb = UILabel()
        lb.text = "关注一些人，回这里看看有什么惊喜  关注一些人，回这里看看有什么惊喜"
        lb.textColor = UIColor.darkGrayColor()
        lb.textAlignment = .Center
        lb.numberOfLines = 0
        return lb
        
    }()
    //注册按钮
   private lazy var registerButton: UIButton = {
       let button = UIButton(type: UIButtonType.Custom)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
    //添加方法
        button.addTarget(self, action: "registerMethod", forControlEvents:UIControlEvents.TouchUpInside)
        return button
    }()
    
    //登录按钮
   private lazy var loginButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
    //添加方法
    button.addTarget(self, action: "loginMethod", forControlEvents: UIControlEvents.TouchUpInside)
    return button
    
    }()

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
